﻿var request = false;

function CollapseGroups() {
    var grid = $('[id^=Grid_]').data('kendoGrid');
    grid.tbody.find('>tr.k-grouping-row').each(
        function () {
            grid.collapseGroup(this);
        }
    );
}

function ExpandRows() {
    var grid = $('[id^=Grid_]').data('kendoGrid');
    grid.tbody.find('tr.k-master-row').each(
        function () {
            grid.expandRow(this);
        }
    );
}

function SetStatusColor() {
    KendoGridColorize('[id^=Grid_]', 4);
}

function SetStatusColorDirect() {
    KendoGridColorize('#TestGrid', 6);
}

function SetStatusColorDetails() {
    var grid = $('[id^=TestDetailedResults_]').data('kendoGrid');
    var data = grid.dataSource.data();
    $.each(data, function (i, row) {
        if (row != undefined) {
            if (row.StepStatus.indexOf('Pass') != -1) {
                $('tr[data-uid="' + row.uid + '"] td:nth-child(2)').css("background-color", "lightgreen");
            }
            if (row.StepStatus.indexOf('Fail') != -1) {
                $('tr[data-uid="' + row.uid + '"] td:nth-child(2)').css("background-color", "pink");
            }
            if (row.StepStatus.indexOf('Warning') != -1) {
                $('tr[data-uid="' + row.uid + '"] td:nth-child(2)').css("background-color", "yellow");
            }
        }
    });
}

function KendoGridColorize(gridName, colNum) {
    var grid = $(gridName).data('kendoGrid');
    var data = grid.dataSource.data();
    $.each(data, function (i, row) {
        if (row != undefined) {
            if (row.Status.indexOf('Pass') != -1) {
                $('tr[data-uid="' + row.uid + '"] td:nth-child(' + (colNum + 2) + ')').css("background-color", "lightgreen");
            }
            if (row.Status.indexOf('Fail') != -1) {
                $('tr[data-uid="' + row.uid + '"] td:nth-child(' + (colNum + 2) + ')').css("background-color", "pink");
            }
            if (row.Status.indexOf('Warning') != -1) {
                $('tr[data-uid="' + row.uid + '"] td:nth-child(' + (colNum + 2) + ')').css("background-color", "yellow");
            }
        }
    });
}

function ResizeGrid(gridName) {
    var grid = $(gridName);
    var dataArea = grid.find(".k-grid-content");
    var newGridHeight = $(document).height() - 250;
    var newDataAreaHeight = newGridHeight - 65;
    dataArea.height(newDataAreaHeight);
    grid.height(newGridHeight);
    $(gridName).data("kendoGrid").refresh();
}

function AccOnChange() {
    var curPage = $('#accordion').accordion('option', 'active');
    var header = $('#accordion h3').eq(curPage).text();

    if (header != "Unbinded" && $('#Group option[value="' + header + '"]')[0].disabled != true) {
        $('#Group').val(header);
    }

    UpdateTime();
}

function UpdateTime() {
    window.request = false;
        
    try {
        window.request = new XMLHttpRequest();
    } catch (trymicrosoft) {
        try {
            window.request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (othermicrosoft) {
            try {
                window.request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                window.request = false;
            }
        }
    }

    if (request) {
        var profiles = $('[id^=chk_]:checked');
        var profileNames = new Array();

        if (profiles.length > 0) {
            profiles.each(function (i, d) {
                profileNames[i] = d.name.split('_')[1];
            });
        } else {
            window.remTime = 0;
            UpdateRemTime();
        }

        var url = "/Execute/UpdateTime?g=" + $('#Group option:selected').val() + "&distribution=" + $('#Distribution option:selected').val() + "&enableSLM=" + $('#enableSLM')[0].checked + "&p=" + profileNames;

        request.open("GET", url, true);
        request.onreadystatechange = UpdateRemTime;
        request.send(null);
    }
}

function UpdateRemTime() {
    if (window.request.readyState == 4 && window.request.status == 200) {
        document.getElementById('runtime').innerHTML = (window.request.responseText) ? window.request.responseText : '0';
    }
}

function ShowVersionPopup(item, force) {
    $(document).ready(function () {
        $("#btnClose").click(function (e) {
            hideDialog();
            e.preventDefault();
        });

        $("#btnSubmit").click(function (e) {
            $("#DXVersion").val($("#ver").val());
            hideDialog();
            e.preventDefault();
        });
    });

    function showDialog(modal) {
        $("#overlay").show();
        $("#dialog").fadeIn(300);

        if (modal) {
            $("#overlay").unbind("click");
        }
        else {
            $("#overlay").click(function (e) {
                hideDialog();
            });
        }
    }

    function hideDialog() {
        $("#overlay").hide();
        $("#dialog").fadeOut(300);
    }

    if (force == true || (item.checked == true && item.attributes.profileType.value != "DX"))
        showDialog(true);
}
